let app = getApp()
Page({

  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.request({
        url: app.globalData.siteroot,
        data: {
          do:'Index_index',
          staff_id:wx.getStorageSync('staff_bind_id')
        },
        success: function(res) {
          that.setData({
            pageData:res.data.info
           })
        }
      })
      wx.request({
        url: app.globalData.siteroot,
        data: {
          do:'Member_staffInfo',
          staff_id:wx.getStorageSync('staff_bind_id')
        },
        success: function(res) {
            that.setData({
              userInfo:res.data.info
            })
            wx.setStorageSync('staff_user', res.data.info)
        }
      })
  },
  return:function(){
    wx.navigateBack({
      delta:1,
      success: function() {
          console.log('成功！')
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.wssInit()
    wx.request({
      url: app.globalData.siteroot, 
      data: {
          do:'Member_staffInfo',
          staff_id:wx.getStorageSync('staff_bind_id')
      },
      success: function(res) {
        wx.setStorageSync('staff_chatid',res.data.info.chatid)
      }
    })
  },
  wssInit () {
    var that = this;
      this.connectWss();
      // 链接失败显示
      wx.onSocketError(function (res) {
        console.log('WebSocket连接打开失败，请检查！', res);
        that.setData({
          isOpen: false,
          statusMsg: '已断开'
        });
      });
      // 监听连接成功
      wx.onSocketOpen(function (res) {
        console.log('WebSocket连接已打开！')
        that.setData({
          isOpen: true,
          statusMsg: '已连接'
        });
        wx.onSocketMessage(function (res) {
          console.log('收到服务器内容：', res);
          var res_data = JSON.parse(res.data);
          switch (console.log(res_data), res_data.type) {
            case "init":
              console.log('bind')
              break;

            case "ping":
              console.log('ping')
              break;

            case "notice":
              console.log('notice')
              break;

            default:
              console.log('文字')
          }
        });
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Member_staffInfo',
            staff_id:wx.getStorageSync('staff_bind_id')
          },
          success: function(res) {
            that.setData({
              staff_chatid:res.data.info.chatid
            })
            wx.setStorageSync('staff_chatid',res.data.info.chatid)
            wx.sendSocketMessage({
              data: JSON.stringify({
                type:"bind",
                uid:res.data.info.chatid,
                toid:'',
                msg:''
              }),
              success: res => {
                console.log(res+'发送成功')
              },
              fail: err=> {
                console.log(err+'失败')
              }
            });
          }
        })
      
      })
    },

  connectWss: function () {
      this.setData({
        statusMsg: '连接中。。。'
      });
      connect();
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  lookmore:function(){
    var that=this
    wx.request({
      url: app.globalData.siteroot,
       data: {
         do:'Message_getPushTmplIds',
         temp_name:'leave_message,place_order,visit_message'
      },
     success: function(res) {
        var leave_message=res.data.info.leave_message
        wx.requestSubscribeMessage({
            tmplIds: [leave_message],
            success (res) {
              wx.request({
                url: app.globalData.siteroot,
                 data: {
                   do:'Member_subscribeTimes',
                   subscribe_data:[{"leave_message":1},{"place_order":0},{"visit_message":0}],
                   staff_id:wx.getStorageSync('staff_bind_id')
                },
               success: function(res) {
                  wx.navigateTo({
                      url: '../staff_statistical/statistical?status=1&type=video',
                 })
               }
              })
            }
        })
     }
    })
  },
  shareset:function(e){
     console.log(e.detail)
  },
  onShareAppMessage: function (e) {
    var sharepath="/business-service/pages/staff_newIndex/index";
    console.log('id:'+e.target.dataset.id)
    if(typeof(e.target.dataset.id)!='undefined'){
      sharepath='/yb_shopv2/entry/video-market/index?id='+e.target.dataset.id;
    }
    return console.log(e, "页面内按钮分享"), {
      title:e.target.dataset.name,
      imageUrl:e.target.dataset.img,
      path:sharepath
  };
  }
})
function connect() {
  wx.connectSocket({
    url:"wss://sc.missyoumuch.cn/vwss",
    success: function (res) {
      console.log('连接成功');
      app.globalData.socketHeartTimer = setInterval(() => {
        var msg = JSON.stringify({
          'data': 'ping'
        })
        wx.sendSocketMessage({
          data: msg,
          success: function (res) {
            console.log('发送心跳')
          },
          fail: function (res) { console.log('心跳发送失败')}
        })
      }, 9000)
    }
  });
}